var Page = {
    init: function () {
        BT.context('storageName', 'tp_' + BT.context('productId') + '_order');
        var location_search = location.search,
            type_index = location_search.indexOf('&type'),
            trim_search = location_search.substring(type_index, 0);
        BT.context('orderURL', '/tp/order/' + trim_search);
        if ( BT.context('type') == 'coupon' ) {
            Page.couponFormEvents();
        } else if ( BT.context('type') == 'points' ) {
            Page.pointsFormEvents();
        };
    },
    pointsFormEvents: function () {
        this.renderPointsForm();
        $('#points-form').on('submit', Page.verifyPoints);
        $('#points-form .btn-primary').on('click', Page.verifyPoints);
    },
    couponFormEvents: function () {
        this.renderCouponForm();
        $('#coupon-form').on('submit', Page.verifyCoupon);
        $('#coupon-form .btn-primary').on('click', Page.verifyCoupon);
        $('#coupon-form .icon-del').on('click', Page.removeCoupon);
    },
    renderPointsForm: function () {
        Page.getLocalStorage();
        if ( BT.context('storageObj').use_points > 0 ) {
            $('#use-points').val(BT.context('storageObj').use_points);
        }
        BT.context('avaliableUserPoints', BT.context('storageObj').avaliable_user_points)
        $('#avaliable-user-points').html(BT.context('avaliableUserPoints'));

    },
    renderCouponForm: function () {
        Page.getLocalStorage();
        if ( BT.context('storageObj').coupon_code ) {
            $('#coupon-code').val(BT.context('storageObj').coupon_code).parent('p').addClass('use');
        }
    },
    removeCoupon: function () {
        if ( window.confirm('放弃使用优惠券？') ) {
            $('#coupon-code').val('').parent('p').removeClass('use');
            BT.context('storageObj').coupon_discount = 0;
            BT.context('storageObj').coupon_code = '';
            Page.setLocalStorage(function () {
                window.location.href = BT.context('orderURL');
            });
        }
    },
    setLocalStorage: function (callback) {
        // 写入 localStorage
        localStorage[BT.context('storageName')] = JSON.stringify(BT.context('storageObj'));
        callback && callback();
    },
    getLocalStorage: function (callback) {
        // 从 localStorage 中取出订单数据
        var storageObj = JSON.parse(localStorage.getItem(BT.context('storageName' ))) || {};
        BT.context('storageObj', storageObj)
        callback && callback();        
    },
    verifyPoints: function (e) {
        e.preventDefault();
        var storageName = 'tp_' + BT.context('productId') + '_order';
        // 验证最大使用积分
        var usePoints = $('#use-points').val();
        usePoints = usePoints.replace(/[^0-9\.]/g,'');
        if ( usePoints.length == 0 ){
            $('#use-points').focus();
            return false;
        }
        usePoints = parseInt(usePoints, 10);
        if ( usePoints > BT.context('avaliableUserPoints') ) {
            usePoints = BT.context('avaliableUserPoints');
            $('#use-points').val(BT.context('avaliableUserPoints'));
        }
        BT.context('storageObj').use_points = usePoints;
        Page.setLocalStorage(function () {
            window.location.href = BT.context('orderURL');
        });
    },
    verifyCoupon: function (e) {
        e.preventDefault();
        var coupon = $('#coupon-code').val();
        if ( coupon === '' ){
            $('#coupon-code').focus();
            return false;
        }
        Page.getLocalStorage();
        var totalPrice = parseFloat(BT.context('storageObj').total_price);
        $.ajax({
            url: "/tp/order/coupon_code/",
            type: "GET",
            data: {
                cc: coupon, // 优惠码
                tp: totalPrice // total_price
            },
            success: function(resp) {
                if ( resp.errmsg ) {
                    alert(resp.errmsg);
                    $('#coupon-code').focus();
                }else{
                    if (resp.discount > 0) {
                        BT.context('storageObj').coupon_discount = resp.discount;
                        BT.context('storageObj').coupon_code = coupon;
                        Page.setLocalStorage(function () {
                            window.location.href = BT.context('orderURL');
                        });
                    }
                }
            }
        });
    }
};

$(document).ready(function() {
    Page.init();
});